package org.example.dxgi;

import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;

public class Py {
    static CDXG4Py DXGI = CDXG4Py.INSTANCE;
    public static void main(String[] args) {

        // 2. 获取窗口句柄
//        String windowTitle = "wa2 - Py.java";
//        WinDef.HWND hwnd = WindowUtils.findWindowByTitle(windowTitle);
        WinDef.HWND hDesktop = User32.INSTANCE.GetDesktopWindow();
        // 3. 初始化 DXGI
        boolean initSuccess = DXGI.init_dxgi(hDesktop);
        if (!initSuccess) {
            System.err.println("DXGI 初始化失败");
            return;
        }

        // 4. 捕获屏幕区域（示例：捕获 800x600 区域）
        byte[] pixels = grabScreen(0, 0, 1920, 960);
        System.out.println("捕获像素数: " + pixels.length);
    }

    // grabScreen 方法（与之前相同）
    public static byte[] grabScreen(int x, int y, int width, int height) {
        int pixelSize = 4; // RGBA
        int bufferSize = width * height * pixelSize;
        Memory buffer = new Memory(bufferSize);
        Pointer result = DXGI.grab(buffer, bufferSize, x, y, width, height);
        byte[] pixels = new byte[bufferSize];
        if (result != null) {
            result.read(0, pixels, 0, bufferSize);
        }
        return pixels;
    }
}
